package com.chatim.nonamechatim.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chatim.nonamechatim.common.Res;
import com.chatim.nonamechatim.pojo.Message;
import com.chatim.nonamechatim.pojo.User;
import com.chatim.nonamechatim.service.MessageService;
import com.chatim.nonamechatim.vo.UserListVo;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/msg")
public class MessageController {

    @Resource
    private MessageService messageService;

    /**
     * 获取未读消息
     */
    @Transactional
    @GetMapping("/getUnreadMsgById")
    public Res<List<Message>> getUnreadMsgById(@Param("id")Long id) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getTid, id).eq(Message::getStatus, 0);
        List<Message> messageList = messageService.list(queryWrapper);
        // 状态改为已读
        Message updateEntity = new Message();
        updateEntity.setStatus(1);
        messageService.update(updateEntity, queryWrapper);
        return Res.success(messageList);
    }

    /**
     * 添加会话列表
     */
    @GetMapping("/addContact")
    public Res<Boolean> addContact(@Param("fid")Long fid, @Param("tid")Long tid) {
        messageService.addContact(fid, tid);
        return Res.success(true);
    }

    /**
     * 查找会话列表
     */
    @GetMapping("getContact")
    public Res<List<UserListVo>> selectContact(@Param("id")Long id) {
        return Res.success(messageService.selectContact(id));
    }
}
